Skip to content

Latest commit

 

History

History
125 lines (107 loc) · 3.31 KB

File metadata and controls

125 lines (107 loc) · 3.31 KB

[*] Numpy Intro

  • These are basically python list with added features and high speed (as are in cython).
  • Much faster than traditional python list as:
    1. It stores data of same type, unlike python list(because of which python list has to store the object type as well which increases size), so no type-checking in Numpy.
    2. The data stored is in contigous format, so faster access time. (SIMD instruction + More cache hit than cache miss)

[*] Arrays

  • Incase there are elements of varying type in numpy.array then they will be type-casted to highest level type.
    • Mixture of int & float will be typecasted to float.
    • Mixture of int, float & string will be typecasted to string.
>>> import numpy as np
>>> np.array ([[1,2,3,4],[5,6,7,8]])
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
>>> np.array ([[1,2,3,4],[5,6,7,8]], dtype=np.float32)
array([[1., 2., 3., 4.],
       [5., 6., 7., 8.]], dtype=float32)

[*] Copying

  • Similar to python list, assigning a numpy array to a new variable doesn't create a new pbject rather justs increments the reference.
  • Use deepcopy to create a new instance.
>>> import numpy as np
>>> x = np.array ([[1,2,3,4],[5,6,7,8]], dtype=np.float32)
>>> y = x
>>> x is y
True
>>> z = x.copy()
>>> x is z
False
>>> 

[*] Casting

  • Numpy array can be type-casted using astype().
>>> import numpy as np
>>> x = np.array ([[1,2,3,4],[5,6,7,8]], dtype=np.int32)
>>> x
array([[1, 2, 3, 4],
       [5, 6, 7, 8]], dtype=int32)
>>> x = x.astype(np.float32)
>>> x
array([[1., 2., 3., 4.],
       [5., 6., 7., 8.]], dtype=float32)

[*] Nan & Infinity

  • Numpy provides Nan (not a number) as a placeholder for values which doesn't contain any data.
  • To represent infinity, numpy provides np.inf.
  • Useful incase of missing/in-complete data.
  • dtype() for Nan & inf is float64.
>>> a  = np.array([np.nan, 1,2])
>>> a
array([nan,  1.,  2.])

>>> a  = np.array([np.nan, 1,2 , np.inf , -np.inf])
>>> a
array([ nan,   1.,   2.,  inf, -inf])
>>> a.dtype
dtype('float64')

[*] Ranged Data

  • Using numpy range(), similar to python range().
  • It performs upcasting similar to np.array().
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(5.1)
array([0., 1., 2., 3., 4., 5.])
>>> np.arange(-1,4)
array([-1,  0,  1,  2,  3])
>>> np.arange(-2,3,0.5)
array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5,  2. ,  2.5])

[*] Reshape Data

  • It is used to re-organise the numpy array into rows and columns, with a contraint being the total number of elements in older and newer shape must be same.
>>> a = np.arange(-2,3,0.5)
>>> a
array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5,  2. ,  2.5])
>>> a.shape
(10,)
>>> b= a.reshape(5,2)
>>> b
array([[-2. , -1.5],
       [-1. , -0.5],
       [ 0. ,  0.5],
       [ 1. ,  1.5],
       [ 2. ,  2.5]])

# flatten() converts into 1D array.
>>> b.reshape(10) == b.flatten()
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True])

# For data transposition
>>> np.transpose(b)
array([[-2. , -1. ,  0. ,  1. ,  2. ],
       [-1.5, -0.5,  0.5,  1.5,  2.5]])

[*] Zeroes & Ones

  • Creating array with binary data
>>> a = np.zeros(5)
>>> a
array([0., 0., 0., 0., 0.])
>>> a = np.ones(5)
>>> a
array([1., 1., 1., 1., 1.])